﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using InternshipManagement.Helpers;
using InternshipManagement.Models;

namespace InternshipManagement.Controllers
{
    [CustomAuthorize]
    public class ProfileController : Controller
    {
        AccountRepository accountRepository = new AccountRepository();
        SemesterRepository semesterRepository = new SemesterRepository();
        //
        // GET: /Edit/

        public ActionResult StudentEdit()
        {
            if ((int)Session["UserTypeID"] == 1)//TODO
            {
                Student student = accountRepository.GetStudent((int)Session["UserID"]);
                return View(student);
            }
            return View();
        }

        [HttpPost]
        public ActionResult StudentEdit(FormCollection formvalues)
        {

            Student student = accountRepository.GetStudent((int)Session["UserID"]);
            student.UserName = "Dummy";
            if (TryUpdateModel(student, new[] { "Name", "Surname", "Phone1", "Phone2", "Address" }, formvalues))
            {
                accountRepository.Save();
                return RedirectToAction("Index", "Home");
            }

            return View(student);
        }

        public ActionResult StudentChangePassword()
        {
            if ((int)Session["UserTypeID"] == 1)//TODO
            {
                return View();
            }
            return View();
        }

        [HttpPost]
        public ActionResult StudentChangePassword(ChangePasswordModel model)
        {

            if (ModelState.IsValid)
            {
                User user = accountRepository.GetUserByID((int)Session["UserID"]);
                if (model.OldPassword == user.Password)
                {
                    user.Password = model.NewPassword;
                    accountRepository.Save();
                    return RedirectToAction("ChangePasswordSuccess");
                }
                else
                {
                    ModelState.AddModelError("", "The current password is incorrect.");
                }
            }
            return View(model);
        }
        public ActionResult ChangePasswordSuccess()
        {
            return View();
        }
    }
}
